$(document).ready(function () {
    document.body.style.overflowY='auto';
    //获取当前页面
    let nowPage=window.location.href.substring(window.location.href.lastIndexOf("\/")+1,window.location.href.length);
    if(nowPage.indexOf("?")!==-1){
        nowPage=nowPage.substring(0,nowPage.indexOf("?"));
    }
    let isShowPopup=true;
    //判断是否在点开了搜索
    let isSearch=false;
    //改变菜单栏菜单的颜色
    if(nowPage==='index.html'){
        document.getElementById('popup0').style.color='#3aa5c5';
    }
    if(nowPage==='aboutUs.html'){
        document.getElementById('popup1').style.color='#3aa5c5';
    }
    if(nowPage==='medicalService.html'){
        document.getElementById('popup2').style.color='#3aa5c5';
    }
    if(nowPage==='departmentIntroduction.html'){
        document.getElementById('popup3').style.color='#3aa5c5';
    }
    if(nowPage==='clinicDistribution.html'){
        document.getElementById('popup4').style.color='#3aa5c5';
    }
    if(nowPage==='healthDynamics.html'){
        document.getElementById('popup5').style.color='#3aa5c5';
    }
    if(nowPage==='membershipService.html'){
        document.getElementById('popup6').style.color='#3aa5c5';
    }
    //获取顶部标题
    const nowHeadTitle=$(".headTitle").html();
    //点击头部按钮时,隐藏或显示菜单栏
    $(".headBtn").click(function(e){
        //阻止事件冒泡
        e.stopPropagation();
        e.preventDefault()
        //获取预约弹窗的zIndex的值
        let resPopup_zIndex=$("#resPopup").css("z-index");
        //获取菜单栏的zIndex的值
        let popup_zIndex=$("#popupTop").css("z-index");
        // 如果预约弹窗的zIndex大于菜单栏弹窗则提高菜单栏的zIndex的值
        if(resPopup_zIndex>=popup_zIndex){
            $("#popupTop").css("z-index",resPopup_zIndex+1);
        }
        if(isShowPopup){
            $('#popupTop').removeClass('hidePopup');
            //显示菜单栏,判断是否是首页,是首页就改变图片的路径
            if(nowPage==='index.html'){
                $(".headBtnImg").attr("src",'img/closeIcon.png');
            }else{
                $(".headBtnImg").attr("src",'../img/closeIcon.png');
            }
            //展开或者关闭菜单栏时正确显示顶部标题
            $(".headTitle").html('搜索');
            //禁止滚动后方的页面
            document.body.style.overflowY='hidden';
            document.documentElement.style.overflowY='hidden';
        }else{
            $('#popupTop').addClass('hidePopup');
            if(nowPage==='index.html'){
                $(".headBtnImg").attr("src",'img/moreIcon.png');
            }else{
                $(".headBtnImg").attr("src",'../img/moreIcon.png');
            }
            //展开或者关闭菜单栏时正确显示顶部标题
            $(".headTitle").html(nowHeadTitle);
            //允许后方页面滚动
            document.body.style.overflowY='auto';
            document.documentElement.style.overflowY='auto';
        }
        isShowPopup=!isShowPopup;
    })
    //点击医疗服务，显示医疗服务的导航
    $("#popup2").click(function (e) {
        e.preventDefault();
        //隐藏菜单栏
        $("#menuContent").css("display","none");
        //隐藏搜索栏
        $(".popupSearch").css('display',"none");
        //隐藏科室介绍
        $(".depIntro").css("display","none");
        //显示医疗服务
        $(".medSer").css("display","block");
        return false;
    })
    //点击科室介绍，显示科室介绍的导航
    $("#popup3").click(function (e) {
        //隐藏菜单栏
        $("#menuContent").css("display","none");
        //隐藏搜索栏
        $(".popupSearch").css('display',"none");
        //隐藏医疗服务
        $(".medSer").css("display","none");
        //显示科室介绍
        $(".depIntro").css("display","block");
        return false;
    })
    //点击空白处显示菜单
    $("#popupTop").click(function (e) {
        //显示菜单栏
        if (!isSearch) {
            $("#menuContent").css("display", "block");
            //显示搜索栏
            $(".popupSearch").css('display', "block");
            //隐藏医疗服务
            $(".medSer").css("display", "none");
            //显示科室介绍
            $(".depIntro").css("display", "none");
        }
    })
    //搜索的文本框获取焦点时,隐藏菜单内容和顶部
    $("#popupText").focus(function () {
        //获取焦点时，点击空白处没有反应
        isSearch=true;
        $('#popupTop').removeClass('popupAction');
        document.getElementById('menuContent').style.display='none';
        $(".head").css("display","none");
        document.getElementById('popupText').style.width='5.3rem';
        document.getElementById('searchTip').style.display='block';
        if(this.value===''||this.value===null){
            document.getElementById('popupCancelButton').style.display='block';
            document.getElementById('popupSearchButton').style.display="none";
        }
    })
    //点击取消按钮,还原菜单内容和顶部
    $("#popupCancelButton").click(function () {
        //点击空白处显示菜单
        isSearch=false;
        document.getElementById('menuContent').style.display='block';
        $(".head").css("display","block");
        document.getElementById('popupText').style.width='6.3rem';
        document.getElementById('popupCancelButton').style.display='none';
        document.getElementById('popupText').value='';
        document.getElementById('searchTip').style.display='none';
        $('#popupTop').addClass('popupAction');
    })
    //搜索文本框中的值部位空时,切换不同的按钮
    $('#popupText').keyup(function () {
        let textValue=this.value;
        if(textValue===''||textValue===null){
            //为空则切换为取消按钮
            document.getElementById('popupCancelButton').style.display='block';
            document.getElementById('popupSearchButton').style.display="none";
        }else{
            //不为空切换为搜索按钮
            document.getElementById('popupCancelButton').style.display='none';
            document.getElementById('popupSearchButton').style.display="block";
        }
    })
    //点击搜索按钮
    $('#popupSearchButton').click(function () {
        //获取文本框中的值
        let textValue=$("#popupText").attr("value");
        $("#popupText").val("");
        if(nowPage==='index.html'){
            window.location.href="page/searchResults.html"
        }else{
            window.location.href="searchResults.html"
        }

    })
})
